home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  5.5 KB  |  234 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    eval.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include "eval.h"
  35.  
  36. #include "alloc.h"
  37. #include "apply.h"
  38. #include "env.h"
  39. #include "error.h"
  40. #include "list.h"
  41. #include "print.h"
  42. #include "syntax.h"
  43.  
  44. struct eval_stack *eval_stack = 0;
  45.  
  46. /* local function prototypes */
  47. static Object eval_combination (Object obj, int do_apply);
  48.  
  49. /* function definitions */
  50.  
  51. Object
  52. eval (Object obj)
  53. {
  54.     Object val;
  55.  
  56. #ifdef SMALL_OBJECTS
  57.     if (INTEGERP (obj) || IMMEDP (obj)) {
  58.     return (obj);
  59.     }
  60. #endif
  61.  
  62. #ifdef SMALL_OBJECTS
  63.     switch (POINTERTYPE (obj))
  64. #else
  65.     switch (TYPE (obj))
  66. #endif
  67.     {
  68.     case True:
  69.     case False:
  70.     case Integer:
  71. #ifdef BIG_INTEGERS
  72.     case BigInteger:
  73. #endif
  74.     case Ratio:
  75.     case SingleFloat:
  76.     case DoubleFloat:
  77.     case ByteString:
  78.     case SimpleObjectVector:
  79.     case Keyword:
  80.     case Character:
  81.     case EndOfFile:
  82.     case EmptyList:        /* is this right? */
  83.     case ForeignPtr:        /* <pcb> */
  84.     return (obj);
  85.     case Values:
  86.     if (obj == unspecified_object) {
  87.         return obj;
  88.     } else {
  89.         return error ("Trying to eval a values object (this is a bug)",
  90.               obj,
  91.               NULL);
  92.     }
  93.     case Symbol:
  94.     val = symbol_value (obj);
  95.     if (!val) {
  96.         return error ("unbound variable", obj, NULL);
  97.     }
  98.     return (val);
  99.     case Pair:
  100.     return (eval_combination (obj, 0));
  101.     default:
  102.     error ("eval: do not know how to eval object", obj, NULL);
  103.     }
  104. }
  105.  
  106. jmp_buf *the_eval_context = NULL;
  107. static Object the_eval_obj = NULL;
  108.  
  109. extern struct frame *the_env;
  110.  
  111. Object
  112. tail_eval (Object obj)
  113. {
  114.  
  115. #ifdef OPTIMIZE_TAIL_CALLS
  116.     if (PAIRP (obj)) {
  117.     the_eval_obj = obj;
  118.     if (the_eval_context == NULL) {
  119.         error ("tail_eval called without a prior eval in progress.", NULL);
  120.     }
  121.     longjmp (*the_eval_context, 1);
  122.     }
  123. #endif
  124.     /* if it's not a <pair>, then call good old eval. */
  125.     return eval (obj);
  126. }
  127.  
  128. /* <pcb> moved apply here to permit safe tail recursion. */
  129.  
  130. Object
  131. apply (Object fun, Object args)
  132. {
  133.     return eval_combination (cons (fun, args), 1);
  134. }
  135.  
  136. static Object
  137. eval_combination (Object obj, int do_apply)
  138. {
  139.     Object op;
  140.     syntax_fun sf;
  141.     Object fun, args, ret;
  142.     struct frame *old_env;
  143.     struct eval_stack *old_stack;
  144.     jmp_buf *old_context;
  145.     jmp_buf this_context;
  146.     int is_tail_call = 0;
  147.     Object tail_required_values;
  148.     Object tail_rest_values;
  149.  
  150.     ResultValueStack = cons (default_result_value (), ResultValueStack);
  151.  
  152.     old_env = the_env;
  153.     old_stack = eval_stack;
  154.  
  155.     /* save a place for tail_eval to longjmp to later. */
  156.     old_context = the_eval_context;
  157.     the_eval_context = &this_context;
  158.     if (setjmp (this_context) != 0) {
  159.     obj = the_eval_obj;
  160.     eval_stack = old_stack;    /* restore the state of the "eval" stack. */
  161.     is_tail_call = 1;    /* a tail call occurred. */
  162.     do_apply = 0;        /* tail_eval called from apply. */
  163.     }
  164.     if (do_apply) {
  165.     fun = CAR (obj);
  166.     args = CDR (obj);
  167.     push_eval_stack (fun);
  168.     ret = apply_internal (fun, args);
  169.     pop_eval_stack ();
  170.     } else {
  171.     op = CAR (obj);
  172.     if (sf = syntax_function (op)) {
  173.         push_eval_stack (op);
  174.         ret = (*sf) (obj);
  175.         pop_eval_stack ();
  176.     } else {
  177.         fun = eval (CAR (obj));
  178.         push_eval_stack (fun);
  179.         args = map (eval, CDR (obj));
  180.         ret = apply_internal (fun, args);
  181.         pop_eval_stack ();
  182.     }
  183.     }
  184.  
  185.     /* restore previous frame's context. */
  186.     the_eval_context = old_context;
  187.  
  188.     /* here we restore the environment since is not restored via tail calls. */
  189.     if (is_tail_call)
  190.     the_env = old_env;
  191.  
  192.     tail_required_values = CAR (CAR (ResultValueStack));
  193.     tail_rest_values = CDR (CAR (ResultValueStack));
  194.     ResultValueStack = CDR (ResultValueStack);
  195.  
  196.     ret = construct_return_values (ret,
  197.                    tail_required_values,
  198.                    tail_rest_values);
  199.     return ret;
  200. }
  201.  
  202. void
  203. pop_eval_stack (void)
  204. {
  205.     eval_stack = eval_stack->next;
  206. }
  207.  
  208. void
  209. push_eval_stack (Object obj)
  210. {
  211.     struct eval_stack *tmp =
  212.     (struct eval_stack *) checking_malloc (sizeof (struct eval_stack));
  213.  
  214.     tmp->next = eval_stack;
  215.     tmp->context = obj;
  216.     eval_stack = tmp;
  217. }
  218.  
  219. Object
  220. print_stack (void)
  221. {
  222.     struct eval_stack *entry;
  223.     int i;
  224.  
  225.     for (i = 0, entry = eval_stack->next;
  226.      entry != NULL;
  227.      entry = entry->next, i++) {
  228.     fprintf (stderr, "#%d ", i);
  229.     print_object (stderr, entry->context, 1);
  230.     fprintf (stderr, "\n");
  231.     }
  232.     return unspecified_object;
  233. }
  234.